<template>
  <el-form-item label="数值类型">
    <el-select
      v-model="optionModel.valueType"
      @change="() => {
        updateAssignValue('activeValue', 1)
        updateAssignValue('inactiveValue', 2)
      }"
    >
      <el-option
        v-for="(item, index) in valueTypeOptions"
        :key="index"
        :label="item.label"
        :value="item.value"
      >
      </el-option>
    </el-select>
  </el-form-item>
  <el-form-item label="开始时的值">
    <el-input style="width: 100%"
      v-model="optionModel.activeValue"
      @change="updateAssignValue('activeValue', 1)"
    >
    </el-input>
  </el-form-item>
  <el-form-item :label="i18nt('designer.setting.activeText')">
    <el-input v-model="optionModel.activeText"></el-input>
  </el-form-item>
</template>

<script>
  import i18n from "@/utils/i18n"

  export default {
    name: "activeText-editor",
    mixins: [i18n],
    props: {
      designer: Object,
      optionModel: Object,
      selectedWidget: Object,
    },
    watch: {
      selectedWidget: {
        handler(val) {
          const { optionModel } = this
          if (!optionModel.hasOwnProperty("valueType")) {
            this.optionModel.valueType = 'number'
          }
          if (!optionModel.hasOwnProperty("activeValue")) {
            this.optionModel.activeValue = 1
          }
        },
        deep: false,
        immediate: true
      },
    },
    data () {
      return {
        valueTypeOptions: [
          { label: '数字', value: 'number' },
          { label: '字符', value: 'string' },
        ]
      }
    },
    methods: {
      /**
       * 更新的开关组件的值
       * 
       * @param field 字段
       * @param defValue 默认值
       */
      updateAssignValue (field, defValue) {
        const { valueType } = this.optionModel
        const targetValue = this.optionModel[field]
        if (valueType === 'number') {
          if (Number.isNaN(Number(targetValue))) {
            this.optionModel[field] = defValue
          } else {
            this.optionModel[field] = Number(targetValue)
          }
        } else {
          this.optionModel[field] = targetValue + ''
        }
      }
    }
  }
</script>

<style scoped>

</style>
